<?php
class qSession
{
    private $variables = array();
    private static $instance;
    
    private function __construct()
    {
    }
    
    public static function StartSession($sessionname = null)
    {
    	if ($sessionname != null)
    		session_name($sessionname);
    	
        session_start();
        
        if (isset($_SESSION['_qSession']))
        {
            self::$instance = unserialize($_SESSION['_qSession']);
        }
        
        else
        {
            self::$instance = new qSession();
        }
        
        register_shutdown_function(array(self::$instance, 'SaveSession'));
        
        return self::$instance;
    }
    
    public function GetInstance()
    {
        return self::$instance;
    }
    
    public function SaveSession()
    {
        $_SESSION['_qSession'] = serialize($this);
    }
    
    public function Store($name, $val)
    {
        if (is_string($name))
        {
            $this->variables[$name] = $val;
        }
        else if (is_array($name))
        {
            foreach ($name as $elem)
            {
                $this->Store($elem['name'], $elem['val']);
            }
        }
    }
    
    public function Get($name)
    {
        return (isset($this->variables[$name]) ? $this->variables[$name] : false);
    }
    
    public function Delete($name)
    {
        if (isset($this->variables[$name]))
        {
        	unset($this->variables[$name]);    
        	return true;
        }
        
        else
        {
            return false;
        }
    }
    
    public function Destroy()
    {
        session_destroy();
        unset($this);
    }
    
    public function Encode()
    {
    	$this->SaveSession();
        return session_encode();
    }
    
    public function Decode($string)
    {
        session_decode($string);
    }
    
    public static function CookieParams($params = null)
    {
        if (is_array($params))
        {
        	if (isset($params['lifetime']))
        	{
        		if (isset($params['path']))
        		{
        		    if (isset($params['domain']))
        		    {
        		        if (isset($params['secure']))
        		        {
        		            if (isset($params['httponly']))
        		            {
        		                session_set_cookie_params($params['lifetime'],
        		                	$params['path'],
        		                	$params['domain'],
        		                	$params['secure'],
        		                	$params['httponly']);
        		            }
        		            else
        		            {
        		                session_set_cookie_params($params['lifetime'],
        		                	$params['path'],
        		                	$params['domain'],
        		                	$params['secure']);
        		            }
        		        }
        		        else
        		        {
        		            session_set_cookie_params($params['lifetime'],
        		            	$params['path'],
        		            	$params['domain']);
        		        }
        		    }
        		    else
        		    {
        		        session_set_cookie_params($params['lifetime'],
        		        	$params['path']);
        		    }
        		}
        		else
        		{
        		    session_set_cookie_params($params['lifetime']);
        		}    
        	}
        }
        
        return session_get_cookie_params();
    }
    
    function __sleep()
    {
        return array("variables");
    }
}